Passed
Pull Request — master (#288)
by
unknown
02:06
created

GetMonthlyFairCalendarQueryHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 64
dl 0
loc 76
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A execute 0 13 1
A buildLeaves 0 11 2
A buildEvents 0 23 4
A buildworkedFreeDays 0 15 2
1
import { Inject } from '@nestjs/common';
2
import { QueryHandler } from '@nestjs/cqrs';
3
import { IEventRepository } from 'src/Domain/FairCalendar/Repository/IEventRepository';
4
import { IDateUtils } from 'src/Application/IDateUtils';
5
import { ProjectView } from 'src/Application/Project/View/ProjectView';
6
import { TaskView } from 'src/Application/Task/View/TaskView';
7
import { ILeaveRepository } from 'src/Domain/HumanResource/Leave/Repository/ILeaveRepository';
8
import { FairCalendarView } from '../View/FairCalendarView';
9
import { GetMonthlyFairCalendarQuery } from './GetMonthlyFairCalendarQuery';
10
import { Leave } from 'src/Domain/HumanResource/Leave/Leave.entity';
11
import { Event } from 'src/Domain/FairCalendar/Event.entity';
12
13
@QueryHandler(GetMonthlyFairCalendarQuery)
14
export class GetMonthlyFairCalendarQueryHandler {
15
  constructor(
16
    @Inject('IEventRepository')
17
    private readonly eventRepository: IEventRepository,
18
    @Inject('ILeaveRepository')
19
    private readonly leaveRepository: ILeaveRepository,
20
    @Inject('IDateUtils')
21
    private readonly dateUtils: IDateUtils
22
  ) {}
23
24
  public async execute(
25
    query: GetMonthlyFairCalendarQuery
26
  ): Promise<FairCalendarView[]> {
27
    const { date, userId } = query;
28
    const formatedDate = this.dateUtils.format(date, 'y-MM-dd');
29
30
    const [events, leaves] = await Promise.all([
31
      this.eventRepository.findMonthlyEvents(formatedDate, userId),
32
      this.leaveRepository.findMonthlyLeaves(formatedDate, userId)
33
    ]);
34
35
    return [...this.buildEvents(events), ...this.buildLeaves(leaves), ...this.buildworkedFreeDays(date)];
36
  }
37
38
  private buildEvents(events: Event[]): FairCalendarView[] {
39
    const result: FairCalendarView[] = [];
40
41
    for (const event of events) {
42
      const project = event.getProject();
43
      const task = event.getTask();
44
45
      result.push(
46
        new FairCalendarView(
47
          event.getType(),
48
          event.getTime(),
49
          event.getDate(),
50
          event.getSummary(),
51
          event.getId(),
52
          event.isBillable(),
53
          project ? new ProjectView(project.getId(), project.getName()) : null,
54
          task ? new TaskView(task.getId(), task.getName()) : null
55
        )
56
      );
57
    }
58
59
    return result;
60
  }
61
62
  private buildLeaves(leaves: Leave[]): FairCalendarView[] {
63
    const result: FairCalendarView[] = [];
64
65
    for (const leave of leaves) {
66
      result.push(
67
        new FairCalendarView(leave.getType(), leave.getTime(), leave.getDate())
68
      );
69
    }
70
71
    return result;
72
  }
73
74
  private buildworkedFreeDays(date: Date): FairCalendarView[] {
75
    const result: FairCalendarView[] = [];
76
77
    const workedFreeDays = this.dateUtils.getWorkedFreeDays(date.getFullYear()).filter(holiday => holiday.getMonth() === date.getMonth());
78
79
    const sevenHoursInMinutesTime = 7 * 60;
80
81
    for (const day of workedFreeDays) {
82
      result.push(
83
        new FairCalendarView("holiday", sevenHoursInMinutesTime, this.dateUtils.format(day, "yyyy-MM-dd"))
84
      )
85
    }
86
87
    return result;
88
  }
89
}
90